<template>
  <div class="table-main">
    <el-dialog
      @close="handMyClose"
      :close-on-click-modal="false"
      :append-to-body="true"
      title="员工"
      :visible.sync="dialogVisible1"
      width="60%"
    >
      <!-- relacationShops -->
      <div style="margin-top:50px;">
        <el-form ref="form" :model="form" label-width="120px" :rules="rules">
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="昵称/登录名:" prop="userName">
                <el-input :disabled="isShow!=0" v-model="form.userName" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="角色:" prop="roleid">
                <el-select :disabled="isShow==3" v-model="form.roleid" filterable placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.id"
                    :label="item.roleName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="商家店铺:" prop="relacationShops">
                <el-select
                  @change="handIds"
                  filterable
                  multiple
                  :disabled="isShow==3"
                  v-model="form.relacationShops"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in findAllsList"
                    multiple
                    :key="item.id"
                    :label="item.shopName"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>-->
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="联系方式:" prop="phone">
                <el-input :disabled="isShow==3" v-model="form.phone" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="邮箱:" prop="email">
                <el-input :disabled="isShow==3" v-model="form.email" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="isShow==0" :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="密码:" prop="passWord">
                <!-- <el-input :disabled="isShow==3" v-model="form.passWord" placeholder="请输入"></el-input> -->
                <div>
                  <el-input
                    :disabled="isShow==3"
                    :key="passwordType"
                    ref="password"
                    v-model="form.passWord"
                    :type="passwordType"
                    placeholder="密码"
                    name="password"
                    tabindex="2"
                    autocomplete="on"
                    @keyup.native="checkCapslock"
                    @blur="capsTooltip = false"
                  />
                  <span class="show-pwd" @click="showPwd" style="position: absolute; right: 20px;">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                  </span>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="isShow==0" :gutter="20" justify="center" type="flex">
            <el-col :span="16">
              <el-form-item label="确认密码:" prop="checkPass">
                <!-- <el-input :disabled="isShow==3" v-model="form.checkPass" placeholder="请输入"></el-input> -->
                <div>
                  <el-input
                    :disabled="isShow==3"
                    :key="passwordType2"
                    ref="password2"
                    v-model="form.checkPass"
                    :type="passwordType2"
                    placeholder="密码"
                    name="password"
                    tabindex="2"
                    autocomplete="on"
                    @keyup.native="checkCapslock2"
                    @blur="capsTooltip2 = false"
                  />
                  <span class="show-pwd" @click="showPwd2" style="position: absolute; right: 20px;">
                    <svg-icon :icon-class="passwordType2 === 'password' ? 'eye' : 'eye-open'" />
                  </span>
                </div>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible1 = false">取 消</el-button>
        <el-button
          type="primary"
          @click="handFromSub('form')"
          :loading="bntLoading"
          v-if="isShow!=3"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { imgSizi } from '@/utils/myUtils'
import md5 from 'js-md5'
export default {
  name: 'SidebarLogo',
  components: {},
  watch: {
    isState(newObj) {
      this.isShow = newObj
    },
  },
  props: {
    isState: {
      type: Number,
      default: 0,
    },
  },
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.passWord) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      passwordType: 'password',
      passwordType2: 'password',
      capsTooltip: false,
      capsTooltip2: false,
      options: [],
      rules: {
        checkPass: [
          { required: true, validator: validatePass2, trigger: 'blur' },
        ],
        userName: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入电话号码', trigger: 'blur' }],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }],
        passWord: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        roleid: [{ required: true, message: '请选择角色', trigger: 'change' }],
      },

      bntLoading: false,
      loading2: false,
      headers: {
        token: localStorage.getItem('mytoken') || '',
      },
      bannerType: [
        {
          label: '汽车轮播图',
          value: 2,
        },
        {
          label: '商品轮播图',
          value: 1,
        },
        {
          label: '首页弹窗广告',
          value: 3,
        },
      ],
      form: {},
      shopStatusOptions: [],
      isShow: 0,
      imageUrl: '',
      dialogImageUrl: '',
      dialogVisible1: false,
      addressKeyword: '',
      activeName: 'first',
      dialogVisible: false,
      tipShow: false,
      findAllsList: [],
    }
  },
  async mounted() {
    this.findAllsList = (await this.$api.userByShop()).data.data
    this.$api.selectAllRole().then((res) => {
      this.options = res.data.data
    })
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    showPwd2() {
      if (this.passwordType2 === 'password') {
        this.passwordType2 = ''
      } else {
        this.passwordType2 = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password2.focus()
      })
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    checkCapslock2(e) {
      const { key } = e
      this.capsTooltip2 = key && key.length === 1 && key >= 'A' && key <= 'Z'
    },
    handIds(val) {
      console.log(val, 'val')
    },
    uploadChange() {},
    onError() {},
    async handGetShop(id) {
      // this.findAllsList = (await this.$api.getSysFindAlls()).data.data
      // this.$api.selectAllRole().then(res => {
      //   this.options = res.data.data
      // })
      this.$api.selectUserByIdUser({ id }).then((res) => {
        res.data.data.roleid = parseInt(res.data.data.roleid)
        this.form = res.data.data
        if (this.form.relacationShops) {
          this.form.relacationShops = this.form.relacationShops.split(',')
          for (let i = 0; i < this.form.relacationShops.length; i++) {
            this.form.relacationShops[i] = parseInt(
              this.form.relacationShops[i]
            )
          }
        }
        console.log('编辑', this.form.relacationShops, this.findAllsList)
      })
    },
    handSuccess(res, file) {
      var url = JSON.parse(res.message)
      this.$set(this.form, 'resPath', url.url)
      this.loading2 = false
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      const isJPG = file.type === 'image/jpeg'
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
      }
      const isSize = imgSizi(file, 672, 180)

      return isSize
    },
    handMyClose() {
      for (let key in this.form) {
        this.form[key] = ''
      }
      this.$parent.closeFrom()
    },
    handFromSub(formName) {
      //1编辑
      this.form.relacationShops = this.newShopID
      if (this.isState == 1) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.bntLoading = true
            // this.form.relacationShops = this.form.relacationShops.join(',')
            // md5

            this.$api
              .editUser(this.form)
              .then((res) => {
                this.dialogVisible1 = false
                this.$parent.init()

                this.$message({
                  message: res.data.message + ',店铺关联信息在重新登录后生效',
                  type: 'success',
                })
                this.bntLoading = false
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入',
            })
            this.bntLoading = false
            return false
          }
        })
      } else if (this.isState == 0) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // this.form.relacationShops = this.form.relacationShops.join(',')
            this.bntLoading = true
            this.$api
              .addUser(this.form)
              .then((res) => {
                this.dialogVisible1 = false
                this.bntLoading = false
                this.$parent.init()
                this.$refs.form.resetFields()
                console.log(res, '添加', this.form)
                this.$message({
                  message: res.data.message,
                  type: 'success',
                })
              })
              .finally(() => {
                this.bntLoading = false
              })
          } else {
            console.log('error submit!!')
            this.$message({
              message: '请完成表单录入',
            })
            return false
          }
        })
      }
    },

    // handSuccess(response, file, fileList) {
    //   console.log(response, file, fileList, '成功回调')
    //   this.form.shopImg = response.message
    // },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    // handlePictureCardPreview(file) {
    //   this.dialogImageUrl = file.url
    //   this.dialogVisible2 = true
    // },
    handleClose() {
      this.dialogVisible = false
    },
    handMapShow() {
      this.dialogVisible = true
    },
    handleClick() {},
  },
}
</script>

<style lang="scss" scoped>
.map_btn {
  width: 82px;
  height: 22px;
  background: rgba(56, 173, 147, 1);
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  line-height: 22px;
  text-align: center;
  margin-top: 8px;
}
/deep/ .el-form-item__label {
  font-size: 14px;
  color: #000;
  font-weight: bold;
}
.form_box {
  width: 80%;
  margin: 0 auto;
}
.avatar-uploader /deep/ .el-upload {
  border: 1px dashed #38ad93;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #38ad93;
  width: 336px;
  height: 90px;
  line-height: 90px;
  text-align: center;
}
.avatar {
  width: 336px;
  height: 90px;
  display: block;
}
</style>
